package com.spzx.order.configure;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayedMqConfig {

    public static final String exchange_delay = "exchange.delay";
    public static final String routing_delay = "routing.delay";
    public static final String queue_delay_1 = "queue.delay.1";

    //声明队列
    @Bean
    public Queue delayedQueue(){
        return new Queue(queue_delay_1,true);
    }

    //声明交互机
    @Bean
    public CustomExchange delayedExchange(){ //根据消息延迟时间，将消息转发给队列。
        Map args = new HashMap();
        args.put("x-delayed-type","direct"); //  fanout 广播交换机   direct 定向交换机    topic 主题交换机   * 不多不少匹配一个单词  # 匹配零个或多个单词
        return new CustomExchange(exchange_delay,"x-delayed-message",true,false,args);
    }

    //声明交换机与队列的绑定
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(delayedQueue()).to(delayedExchange()).with(routing_delay).noargs();
    }
}
